package com.example.zhihu.service.Imp;

import com.example.zhihu.mapper.AnswerCustomMapper;
import com.example.zhihu.mapper.UserAttentionMapper;
import com.example.zhihu.mapper.UserLikeAnswerMapper;
import com.example.zhihu.mapper.UserMapper;
import com.example.zhihu.pojo.UserAttention;
import com.example.zhihu.pojo.UserLikeAnswer;
import com.example.zhihu.pojo.Users;
import com.example.zhihu.pojo.vo.AnswerVO;
import com.example.zhihu.pojo.vo.UserVO;
import com.example.zhihu.service.UserService;
import com.example.zhihu.utils.MD5Utils;
import com.example.zhihu.utils.PagedResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImp implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserAttentionMapper userAttentionMapper;

    @Autowired
    private UserLikeAnswerMapper userLikeAnswerMapper;

    @Autowired
    private AnswerCustomMapper answerCustomMapper;

    @Autowired
    private Sid sid;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public boolean queryUserIsExit(String username) {
        Users user = new Users();
        user.setAccount(username);
        Users result = userMapper.selectOne(user);
        return result == null?false:true;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void saveUser(Users user) {
        String userId = sid.nextShort();
        user.setId(userId);
        userMapper.insert(user);
    }

    @Override
    public List<UserVO> userList() {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("state",1);
        List<UserVO> userList = userMapper.userList();
        return userList;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUserForLogin(String username, String password) {
        Example example = new Example(Users.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("account",username);
        criteria.andEqualTo("password", password);
        Users result = userMapper.selectOneByExample(example);
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateUser(Users user) {
        Example userExample = new Example(Users.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("id",user.getId());
        if(StringUtils.isNotBlank(user.getPassword())) {
            try {
                String newPass = MD5Utils.getMD5Str(user.getPassword());
                user.setPassword(newPass);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        userMapper.updateByExampleSelective(user,userExample);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Users queryUser(String userId) {
        Example userExample = new Example(Users.class);
        Example.Criteria criteria = userExample.createCriteria();
        criteria.andEqualTo("id", userId);
        Users user = userMapper.selectOneByExample(userExample);
        return user;
    }

    @Override
    public void saveUserFans(String userId, String fanId) {
        UserAttention userAttention = new UserAttention();
        userAttention.setId(sid.nextShort());
        userAttention.setUserId(userId);
        userAttention.setAttentionUserid(fanId);
        userAttention.setCreateTime(new Date());
        userAttentionMapper.insert(userAttention);

        userMapper.addFansCount(userId);
        userMapper.addFollowsCount(fanId);
    }

    @Override
    public void deleteUserFans(String userId, String fanId) {
        Example example = new Example(UserAttention.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("attentionUserid",fanId);
        userAttentionMapper.deleteByExample(example);

        userMapper.reduceFansCount(userId);
        userMapper.reduceFollowsCount(fanId);
    }

    @Override
    public boolean isFollow(String id, String fanId) {
        Example example = new Example(UserAttention.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",id);
        criteria.andEqualTo("attentionUserid",fanId);
        List<UserAttention> list = userAttentionMapper.selectByExample(example);
        if(list != null && !list.isEmpty() && list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isUserLikeAnswer(String loginUserId, String answerId) {
        if (StringUtils.isBlank(loginUserId) || StringUtils.isBlank(answerId)) {
            return false;
        }
        Example example = new Example(UserLikeAnswer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",loginUserId);
        criteria.andEqualTo("answerId",answerId);
        criteria.andEqualTo("isLike",1);
        List<UserLikeAnswer> list = userLikeAnswerMapper.selectByExample(example);
        if(list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean isUserCollectAnswer(String loginUserId, String answerId) {
        if (StringUtils.isBlank(loginUserId) || StringUtils.isBlank(answerId)) {
            return false;
        }
        Example example = new Example(UserLikeAnswer.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",loginUserId);
        criteria.andEqualTo("answerId",answerId);
        criteria.andEqualTo("isCollect",1);
        List<UserLikeAnswer> list = userLikeAnswerMapper.selectByExample(example);
        if(list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    @Override
    public List<AnswerVO> queryUserLike(String userId) {
        List<AnswerVO> list = answerCustomMapper.queryUserLike(userId);
        return list;
    }

    @Override
    public List<AnswerVO> queryUserCollect(String userId) {
        List<AnswerVO> list = answerCustomMapper.queryUserCollect(userId);
        return list;
    }

    @Override
    public List<AnswerVO> queryUserFollow(String userId) {
        List<AnswerVO> list = answerCustomMapper.queryUserFollow(userId);
        return list;
    }
}
